using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using mdryden.Core.Localization;
using LastoRevival.Service;
using AttributeRouting.Web.Mvc;
using LastoRevival.Service.Contracts.Models;
using LastoRevival.Service.Implementations.Exceptions;

namespace LastoRevival.Website.Controllers
{
	public class PostController : LastoControllerBase
	{

		private RedirectToRouteResult GetAfterPostResult(int forumID, int postID)
		{
			if (UserSettings.ReturnToListAfterPosting)
			{
				// return to the forum list
				return RedirectToAction("ViewForum", "Forum", new { forumID = forumID });
			}
			else
			{
				// view the new post
				return RedirectToAction("ViewPost", "Post", new { postID = postID });
			}
		}

		

		[GET("post/{forumId}")]
		public ViewResult CreateForm(int forumID)
		{
			
			// redirect invalid requests
			// this is a thread create call
			if (!UserProfile.CanCreateThread(forumID))
			{
				return AccessError(LanguageLoader.GetWord("NotAuthorizedToPostError"));
			}

			var forum = Service.GetForum(LoginState.Token, forumID);

			// this method (get Create) is always for new threads
			var newThread = new CreatePostModel { ForumID = forumID };

			ViewBag.ForumTitle = forum.Title;

			return View("Create", newThread);
		}

		[POST("post")]
		public ActionResult Create(CreatePostModel post)
		{
			if (ModelState.IsValid)
			{
				// add server side values
				post.IP = Request.ServerVariables["REMOTE_ADDR"];
				post.Client = Request.ServerVariables["HTTP_USER_AGENT"];
				post.DatePosted = DateTime.UtcNow;
				
				try
				{
					var postID = Service.CreatePost(LoginState.Token, post);
					return GetAfterPostResult(post.ForumID, postID);
				}
				catch (NoAccessException)
				{
					return AccessError(LanguageLoader.GetWord("NotAuthorizedToPostError"));
				}
			}
			else
			{
				// not valid
				ViewBag.ForumTitle = Service.GetForum(LoginState.Token, post.ForumID).Title;

				return View(post);
			}
		}

		[GET("edit/{postId}")]
		public ViewResult EditForm(int postID)
		{
			try
			{
				// load post
				var post = Service.GetPostForEditing(LoginState.Token, postID);

				ViewBag.ForumTitle = Service.GetForum(LoginState.Token, post.ForumID).Title;

				return View("Edit", post);
			}
			catch (NoAccessException)
			{
				return AccessError(LanguageLoader.GetWord("NotAuthorizedToEditPostError"));
			}

		}

		[POST("edit")]
		public ActionResult Edit(EditPostModel post)
		{
			if (ModelState.IsValid)
			{
				try
				{
					Service.UpdatePost(LoginState.Token, post);
				}
				catch (NoAccessException)
				{
					return AccessError(LanguageLoader.GetWord("NotAuthorizedToEditPostError"));
				}

				// redirect to edited post
				return RedirectToAction("ViewPost", "Post", new { postID = post.PostID });
			}
			else
			{
				// not valid
				var originalPost = Service.GetPostForEditing(LoginState.Token, post.PostID);
				ViewBag.ForumTitle = Service.GetForum(LoginState.Token, originalPost.ForumID).Title;

				return View("Edit", post);
			}
		}

		[GET("view/{postId}")]
		public ViewResult ViewPost(int postID)
		{
			try
			{
				var post = Service.GetPostForViewing(LoginState.Token, postID);

				ViewBag.ForumTitle = Service.GetForum(LoginState.Token, post.Thread.Details.ForumID).Title;

				return View("View", post);
			}
			catch (NoAccessException)
			{
				return AccessError(LanguageLoader.GetWord("NoAccessToViewPostError"));
			}
		}

		[POST("reply")]
		public ActionResult Reply(CreateReplyModel reply)
		{
			if (ModelState.IsValid)
			{
				// add server side values
				reply.IP = Request.ServerVariables["REMOTE_ADDR"];
				reply.Client = Request.ServerVariables["HTTP_USER_AGENT"];
				reply.DatePosted = DateTime.UtcNow;

				try
				{
					var postID = Service.CreateReply(LoginState.Token, reply);

					return GetAfterPostResult(reply.ForumID, postID);
				}
				catch (NoAccessException)
				{
					return AccessError(LanguageLoader.GetWord("NotAuthorizedToPostError"));
				}
			}
			else
			{
				// not valid
				ViewBag.ForumTitle = Service.GetForum(LoginState.Token, reply.ForumID).Title;

				// set view data for the post we just tried to insert
				ViewBag.FailedReply = reply;

				return ViewPost(reply.ParentPostID);
			}
		}


	}
}
